//
//  HTTPGlobals.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/6.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "HTTPGlobals.h"
#import "Config.h"
@implementation HTTPGlobals
//实例方法
- (HTTPGlobals *)initHTTPGlobals {
    self = [super init];
    if (self) {
        _manager = [AFHTTPRequestOperationManager manager];
    }
    return self;
}
//类方法
+(HTTPGlobals *)shareHTTPGlobal {
    HTTPGlobals *status = [[self alloc] initHTTPGlobals];
    return status;
}
/**
 *  单利方法
 */
//+(HTTPGlobals *)shareHTTPGlobal {
//    static HTTPGlobals *this = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        if (this == nil) {
//            this = [[HTTPGlobals alloc] init];
//            this.manager = [AFHTTPRequestOperationManager manager];
//        }
//    });
//    
//    return this;
//}
-(void)GetUserHTTPRequest:(NSString *)URL parameters:(NSDictionary *)params finish:(void(^)(BOOL seccess,NSString *error,id obj))finish {
    NSString *url = [NSString stringWithFormat:@"%@%@",@"http://yunku.mobi/index.php/",URL];
    [self.manager GET:url parameters:params success:^(AFHTTPRequestOperation * operation, id obj) {
        
        finish(YES,@"请求成功",obj);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        finish(NO,@"请求失败",nil);
        DLog(@"error = %@",error);
    }];

}

-(void)GetHTTPRequest:(NSString *)URL parameters:(NSDictionary *)params finish:(void (^)(BOOL, NSString *error, id))finish {
    NSString *url = [NSString stringWithFormat:@"%@%@",KBSSDKAPIYunPingTaiPart81,URL];
    [self.manager GET:url parameters:params success:^(AFHTTPRequestOperation * operation, id obj) {
        
        finish(YES,@"请求成功",obj);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        finish(NO,@"请求失败",nil);
        DLog(@"error = %@",error);
    }];
    
}

-(void)PostHTTPRequest:(NSString *)URL parameters:(NSDictionary *)params finish:(void (^)(BOOL, NSString * error, id))finish {
    NSString *url = [NSString stringWithFormat:@"%@%@",KBSSDKAPIYunPingTaiPart81,URL];
//    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    self.manager. requestSerializer = [ AFHTTPRequestSerializer serializer ];
//    
//    self.manager. responseSerializer = [ AFHTTPResponseSerializer serializer ];
    [self.manager POST:url parameters:params success:^(AFHTTPRequestOperation * operation, id obj) {
//        DLog(@"operation.str = %@",operation.responseString);
        finish(YES,@"请求成功",obj);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        finish(NO,@"请求失败",nil);
        DLog(@"error = %@",error);
    }];
}

-(void)uploadImageWithUrl:(NSString *)url
                    image:(UIImage *)image
                imageName:(NSString *)imageName
               parameters:(NSDictionary *)params
                   finish:(void(^)(BOOL seccess,NSError *error,id obj))finish {
    NSString *URL = [NSString stringWithFormat:@"%@%@",KBSSDKAPIYunPingTaiPart81,url];
    [self.manager POST:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
        // 上传图片，以文件流的格式
        //name是服务器指定的文件夹
        [formData appendPartWithFileData:imageData name:imageName fileName:fileName mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation * operation, id responseObject) {
        finish(YES,nil,responseObject);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        finish(NO,error,nil);
    }];
}

-(void)uploadImageWithUrl:(NSString *)url
                 imageArr:(NSArray *)imageArr
             imageNameArr:(NSArray *)imageNameArr
               parameters:(NSDictionary *)params
                   finish:(void(^)(BOOL seccess,NSError *error,id obj))finish {
    NSString *URL = [NSString stringWithFormat:@"%@%@",KBSSDKAPIYunPingTaiPart81,url];
//    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    self.manager. requestSerializer = [ AFHTTPRequestSerializer serializer ];
//    
//    self.manager. responseSerializer = [ AFHTTPResponseSerializer serializer ];
    [self.manager POST:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    
        for (int i = 0;i<imageArr.count;i++) {
            UIImage *image = imageArr[i];
            NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
            // 上传图片，以文件流的格式
            //name是服务器指定的文件夹
            [formData appendPartWithFileData:imageData name:imageNameArr[i] fileName:fileName mimeType:@"image/png"];
        }
       
    } success:^(AFHTTPRequestOperation * operation, id responseObject) {
        finish(YES,nil,responseObject);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        finish(NO,error,nil);
    }];

}

@end
